import * as utils from '../utils'

export const initFormConfig = (formItem: any) => {
  return {
    title: formItem.formTypeName,
    tips: '', // 提示语
    required: false, // 是否必填
    key: formItem.id, // 表单项key值

    sourceKey: '', // 数据源表单项Key
    formatFns: [] // 数据匹配方案
  }
}

// 初始化解析规则
export const initInputConfig = (key: string) => {
  return {
    name: '', // 解析值
    rule: '' // 规则
  }
}

// 获取数据源的值
export const getSoureValue = (sourceKey: string, forms: any, formValueList: any) => {
  const formItem = forms.find((el: any) => el.config.key === sourceKey)
  const formValue = formValueList.find((el: any) => el.key === sourceKey)
  const formConfig = formItem.config
  return utils.getInputValue(formItem.formType, formValue, formConfig)
}

// 根据数据源解析
export const formatValue = (formatFns: any[], val: any) => {
  for (let i = 0; i < formatFns.length; i++) {
    const fnStr = 'return value' + formatFns[i].rule
    const fn = new Function('value', fnStr)
    if (fn(val)) {
      return formatFns[i].name
    }
  }
}
